//
//  IHomeRecommendCateView_V2.swift
//  LeGou
//
//  Created by lxm on 2022/10/21.
//

import UIKit

class HomeRecommendCateView_V2: UIView, UIScrollViewDelegate {
    
    public var itemClick : ((_ index : Int, _ item : HomeCate)->())?
    
    var dataSorce = [HomeCate]() {
        didSet {
            setUI()
        }
    }
    
//    var column = 5
    
    var itemHeight : CGFloat = 60
    
    var inset = UIEdgeInsets(top: 0, left: rpx(23), bottom: 0, right: rpx(23))
    
    var itemWidth : CGFloat = 48
    
    var space : CGFloat {
        get {
            return AppHelper.appVerify() ? rpx(35) : rpx(26)
        }
    }
    
    var lineSpace : CGFloat = 8
    
    let scrollView = UIScrollView()
    
    let indexContainer = UIView().then { v in
        v.backgroundColor = UIColor.zm.color(hexString: Style.mainColorStr, 0.2)
        v.layer.cornerRadius = 2
        v.layer.masksToBounds = true
    }
    
    let indexView = UIView().then { v in
        v.backgroundColor = Style.mainColor
        v.layer.cornerRadius = 2
        v.layer.masksToBounds = true
    }
    
    let bottomView = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        indexContainer.tz_centerX = zm.width / 2
    }
    
    func setUI(){
        addSubview(scrollView)
        
        scrollView.subviews.forEach({$0.removeFromSuperview()})
        scrollView.showsHorizontalScrollIndicator = false
        
        // 行
        let row = 2
        // 列
        let column = (dataSorce.count + 1) / 2
        
        for i in 0 ..<  dataSorce.count {
            let data = dataSorce[i]
//            let width = SCREEN_W / CGFloat(column)
//            let height = rpx(102)
            let frame = CGRect(x: inset.left + CGFloat(i % column) * (itemWidth + space) , y: inset.top + CGFloat(i / column) * (itemHeight + lineSpace) , width: itemWidth, height: itemHeight)
            var v = UIView()
            v = itemView(imgStr: data.detailInfo().icon, title: data.detailInfo().title, frame: frame,tag : i)
            scrollView.addSubview(v)
        }
        
        
        let contentHeight = (itemHeight * CGFloat(row)) + inset.top + inset.bottom + (CGFloat(row) - 1) * lineSpace
        let contentWidth = CGFloat(column) * (itemWidth + space) + inset.left + inset.right
        
        scrollView.contentSize = CGSize(width: contentWidth , height: contentHeight)
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(contentHeight)
        }
        
        scrollView.bounces = false
        scrollView.layer.masksToBounds = false
        
//        backgroundColor = .green
        
//        snp.makeConstraints { make in
//            make.height.equalTo(170)
//        }
//
        addSubview(indexContainer)
        indexContainer.addSubview(indexView)
        let indexContainerW : CGFloat = rpx(24)
        let indexH : CGFloat = 4
        
        indexContainer.frame = CGRect(x: 0, y: 8, width: indexContainerW, height: indexH)
        
        let indexW = SCREEN_W / contentWidth * indexContainerW

        indexView.frame = CGRect(x: 0, y: 0, width: indexW, height: indexH)

        
//        indexContainer.snp.makeConstraints { make in
//            make.bottom.equalToSuperview().offset(10)
//            make.centerX.equalToSuperview()
//            make.height.equalTo(4)
//            make.width.equalTo(indexContentWidht)
//        }
        addSubview(bottomView)
        
        scrollView.delegate = self
        
        bottomView.addSubview(indexContainer)
        
        bottomView.snp.makeConstraints { make in
            make.top.equalTo(scrollView.snp.bottom)
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(12)
        }
        scrollView.layoutIfNeeded()
        bottomView.layoutIfNeeded()
        layoutIfNeeded()
    }
    
    func itemView(imgStr:String, title:String, frame : CGRect,tag : Int) -> UIView {
        let v = UIView(frame: frame)
        let imgW : CGFloat = 40
        let img = UIImageView(frame: CGRect(x: (frame.size.width - imgW) / 2, y: 0, width: imgW, height: imgW))
        img.contentMode = .scaleAspectFit
        img.layer.masksToBounds = true
        
        if imgStr.count > 0 {
            if imgStr.contains("http") {
                img.kf.setImage(with: URL(string: imgStr.zm.aliImg(h: img_low)), placeholder: UIImage(named: "def_img"))
            } else {
                img.image = UIImage.init(named: imgStr)
            }
        } else {
            img.image = UIImage(named: "def_img")
        }
        let label = ZMUtil.label(title: title, color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12, weight: .medium))
        
        v.addSubview(img)
        v.addSubview(label)
//        if imgStr == "home_0buy" {
//            let topIcon = ZMUtil.imageView(imageName: "home_mfs")
//            v.addSubview(topIcon)
//            topIcon.snp.makeConstraints { make in
//                make.bottom.equalTo(img.snp.top).offset(12)
//                make.right.equalTo(img.snp.right).offset(7)
//            }
//        }
        label.snp.makeConstraints { (make) in
            make.top.equalTo(img.snp.bottom).offset(4)
            make.centerX.equalToSuperview()
//            make.width.equalToSuperview()
        }
        label.textAlignment = .center
        // 按钮
        let btn = UIButton()
        v.addSubview(btn)
        btn.style.snpEqualSuper()
        btn.tag = tag
        btn.addTarget(self, action: #selector(itemClick(_:)), for: .touchUpInside)
        label.sizeToFit()
        return v
    }
    
    @objc func itemClick(_ sender : UIButton) {
        if let itemClick = itemClick {
            itemClick(sender.tag, dataSorce[sender.tag])
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let k = scrollView.contentSize.width - SCREEN_W
        let r = scrollView.contentOffset.x / k
        let sk = indexContainer.zm.width - indexView.zm.width
        indexView.zm.setX(sk * r)
    }
}
